package com.my.gesturelockandwakedemo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.my.gesturelockandwakedemo.R;
import com.my.gesturelockandwakedemo.util.CacheActivity;
import com.my.gesturelockandwakedemo.util.SharedPrefUtil;
import com.my.gesturelockandwakedemo.view.GestureLockLayout;

public class CheckGestureLockActivity extends BaseActivity {
    private GestureLockLayout mGestureLockLayout;
    private TextView tvCurrentNum;
    private SharedPrefUtil sp;
    /****/
    private String strGesturePassword;
    private Handler mHandler = new Handler();
    /*** 手势验证次数 */
    private int checkCurrent = 5;
    /*** Toast */
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_gesture_lock);
        sp = new SharedPrefUtil(this, "MyGestureLock");
        // strGesturePassword = "[0,1,2]";
        Log.i("mwbm", "CheckGestureLockActivity onCreate");

        initViews();
        initOnClickEvents();
    }

    @Override
    public void initViews() {
        super.initViews();
        tvCurrentNum = (TextView) findViewById(R.id.tv_current_num_cg);
        mGestureLockLayout = (GestureLockLayout) findViewById(R.id.gl_check);
        // 设置手势解锁的行数列数
        mGestureLockLayout.setDotCount(3);
        // 密码模式，验证密码
        mGestureLockLayout.setMode(GestureLockLayout.VERIFY_MODE);
        // 设置最大尝试次数
        mGestureLockLayout.setTryTimes(checkCurrent);
        // 设置密码
        strGesturePassword = sp.getString("MyGesture", "");
        mGestureLockLayout.setAnswer(strGesturePassword);
        tvCurrentNum.setText("当前剩余次数5次");
    }

    @Override
    public void initOnClickEvents() {
        super.initOnClickEvents();
        mGestureLockLayout.setOnLockVerifyListener(new GestureLockLayout.OnLockVerifyListener() {
            @Override
            public void onGestureSelected(int id) {

            }

            @Override
            public void onGestureFinished(boolean isMatched) {
                if (isMatched) {
                    if (toast != null) {
                        toast.cancel();
                    }

                    Toast.makeText(CheckGestureLockActivity.this, "手势正确", Toast.LENGTH_SHORT).show();
                    resetGesture();
                    CacheActivity.finishSingleActivity(CheckGestureLockActivity.this);
                } else {
                    Toast.makeText(CheckGestureLockActivity.this, "手势错误", Toast.LENGTH_SHORT).show();
                    resetGesture();
                    if (checkCurrent > 0) {
                        checkCurrent--;
                        tvCurrentNum.setText("当前剩余次数" + checkCurrent + "次");
                    } else if (checkCurrent == 0) {
                        checkCurrent = 0;
                        tvCurrentNum.setText("当前剩余次数" + checkCurrent + "次");
                    }
                }
            }

            @Override
            public void onGestureTryTimesBoundary() {
                Toast.makeText(CheckGestureLockActivity.this, "手势验证次数已超上限,退出登录", Toast.LENGTH_LONG).show();
                mGestureLockLayout.setTouchable(false);
                // 清除所有的数据
                sp.clear();
            }
        });
    }


    /**
     * 重置
     */
    private void resetGesture() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mGestureLockLayout.resetGesture();
            }
        }, 500);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            CacheActivity.finishActivity();
            overridePendingTransition(0, R.anim.top_to_bottom);
            Log.i("mwbm", "cgla back");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
